package cn.iocoder.yudao.module.design.controller.admin.houseUserRel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.houseUserRel.vo.*;
import cn.iocoder.yudao.module.design.convert.houseUserRel.HouseUserRelConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.houseUserRel.HouseUserRelDO;
import cn.iocoder.yudao.module.design.service.houseUserRel.HouseUserRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 住宅相关用户关联")
@RestController
@RequestMapping("/design/house-user-rel")
@Validated
public class HouseUserRelController {

    @Resource
    private HouseUserRelService houseUserRelService;

    @PostMapping("/create")
    @Operation(summary ="创建住宅相关用户关联")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:create')")
    public CommonResult<Long> createHouseUserRel(@Valid @RequestBody HouseUserRelCreateReqVO createReqVO) {
        return success(houseUserRelService.createHouseUserRel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新住宅相关用户关联")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:update')")
    public CommonResult<Boolean> updateHouseUserRel(@Valid @RequestBody HouseUserRelUpdateReqVO updateReqVO) {
        houseUserRelService.updateHouseUserRel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除住宅相关用户关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:delete')")
    public CommonResult<Boolean> deleteHouseUserRel(@RequestParam("id") Long id) {
        houseUserRelService.deleteHouseUserRel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得住宅相关用户关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:query')")
    public CommonResult<HouseUserRelRespVO> getHouseUserRel(@RequestParam("id") Long id) {
        HouseUserRelDO houseUserRel = houseUserRelService.getHouseUserRel(id);
        return success(HouseUserRelConvert.INSTANCE.convert(houseUserRel));
    }

    @GetMapping("/list")
    @Operation(summary ="获得住宅相关用户关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:query')")
    public CommonResult<List<HouseUserRelRespVO>> getHouseUserRelList(@RequestParam("ids") Collection<Long> ids) {
        List<HouseUserRelDO> list = houseUserRelService.getHouseUserRelList(ids);
        return success(HouseUserRelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得住宅相关用户关联分页")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:query')")
    public CommonResult<PageResult<HouseUserRelRespVO>> getHouseUserRelPage(@Valid HouseUserRelPageReqVO pageVO) {
        PageResult<HouseUserRelDO> pageResult = houseUserRelService.getHouseUserRelPage(pageVO);
        return success(HouseUserRelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出住宅相关用户关联 Excel")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:export')")
   
    public void exportHouseUserRelExcel(@Valid HouseUserRelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HouseUserRelDO> list = houseUserRelService.getHouseUserRelList(exportReqVO);
        // 导出 Excel
        List<HouseUserRelExcelVO> datas = HouseUserRelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "住宅相关用户关联.xls", "数据", HouseUserRelExcelVO.class, datas);
    }

    @PostMapping("/getUserRelList")
    @Operation(summary ="获得住宅相关用户关联列表")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:query')")
    public CommonResult<List<UserRelListVO>> getUserRelList(@RequestBody HouseUserRelBaseVO vo ) {
        List<UserRelListVO> list = houseUserRelService.getUserRelList(vo );
        return success(list);
    }

    @PostMapping("/getUserRelGoods")
    @Operation(summary ="获取供应商商品列表")
    @PreAuthorize("@ss.hasPermission('design:house-user-rel:query')")
    public CommonResult<List<Map>> getUserRelGoods(@RequestBody HouseUserRelBaseVO vo ) {
        List<Map> list =  new ArrayList<>();
        return success(list);
    }

    @GetMapping("/getUserRelGroupConcatList/{id}")
    @Operation(summary ="获取服务团队处理人角色合并")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:house:query')")
    public CommonResult<List<UserRelListVO>> getUserRelGroupConcatList(@PathVariable Long id) {

        List<UserRelListVO> list = houseUserRelService.getUserRelGroupConcatList(id );

        return success(list);
    }




}
